package aoj.no_1100;

import java.util.Scanner;

public class No_1109 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(true) {
			int z = sc.nextInt();
			if(z == 0)
				break;
			
			int z3 = z*z*z;
			int max = 0;
			for(int x = 1; x <= z; x++) {
				for(int y = 1; y <= z; y++) {
					int x3 = x*x*x;
					int y3 = y*y*y;
					if(x3 + y3 > z3)
						continue;
					
					if(x3 + y3 > max)
						max = x3 + y3;
				}	
			}
			System.out.println(z3 - max);
		}
	}
}
/*
6
4
2
0
*/